package com.beagle.njddapp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.lifecycle.ViewModel;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.RoleBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.ui.presenter.ChooseRolePresenter;
import com.beagle.njddapp.ui.view.CHooseRoleView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.beagle.njddapp.core.UserService.ROLE_DIRVER;
import static com.beagle.njddapp.core.UserService.ROLE_OIL;
import static com.beagle.njddapp.core.UserService.ROLE_OWNER;


/**
 * @author hxx
 * @date 2021-03-10 19:30
 * 选择角色
 */
public class ChangeRoleActivity extends BaseMvpActivity<CHooseRoleView, ChooseRolePresenter> implements CHooseRoleView {

    @BindView(R.id.rl_role_one)
    LinearLayout rlOne;
    @BindView(R.id.rl_role_two)
    RelativeLayout rlTwo;
    @BindView(R.id.rl_role_three)
    RelativeLayout rlThree;
    @BindView(R.id.rl_role_four)
    RelativeLayout rlFour;
    @BindView(R.id.tv_title_one)
    TextView tvTitleOne;
    @BindView(R.id.tv_content_one)
    TextView tvCOntentOne;
    @BindView(R.id.tv_title_two)
    TextView tvTitleTwo;
    @BindView(R.id.tv_content_two)
    TextView tvContentTwo;
    @BindView(R.id.tv_title_three)
    TextView tvTitleThree;
    @BindView(R.id.tv_content_three)
    TextView tvContentThree;
    @BindView(R.id.tv_title_four)
    TextView tvTitleFour;
    @BindView(R.id.tv_content_four)
    TextView tvContentFour;
    @BindView(R.id.tv_now_two)
    TextView tvNowTwo;
    @BindView(R.id.tv_now_three)
    TextView tvNowThree;
    @BindView(R.id.tv_now_four)
    TextView tvNowFour;
    private int roleId;
    private int nowRole;
    private int lastRole = ROLE_OWNER;
    private Drawable seleced, normal;
    private int color_white, color_normal_title, color_content_normal;

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_role;
    }


    @Override
    public void init() {
        StatusBarUtil.immersive(this, Color.WHITE);
        StatusBarUtil.darkMode(this);
        seleced = getResources().getDrawable(R.mipmap.choice_bg_block_sel);
        normal = getResources().getDrawable(R.mipmap.choice_bg_block_nor);
        color_white = Color.WHITE;
        color_normal_title = Color.parseColor("#979EA8");
        color_content_normal = Color.parseColor("#CFD7E3");
        nowRole = UserService.getInstance().getRole();
        roleId=nowRole;
        if (nowRole == ROLE_OWNER) {
            tvNowTwo.setVisibility(View.VISIBLE);
        } else if (nowRole == ROLE_OIL) {
            lastRole = ROLE_OIL;
            tvNowFour.setVisibility(View.VISIBLE);
            rlFour.setBackground(seleced);
            rlTwo.setBackground(normal);
            rlThree.setBackground(normal);
            tvTitleFour.setTextColor(color_white);
            tvContentFour.setTextColor(color_white);
            tvTitleTwo.setTextColor(color_normal_title);
            tvContentTwo.setTextColor(color_content_normal);
            tvTitleThree.setTextColor(color_normal_title);
            tvContentThree.setTextColor(color_content_normal);
        } else if (nowRole == ROLE_DIRVER) {
            lastRole = ROLE_DIRVER;
            tvNowThree.setVisibility(View.VISIBLE);
            rlThree.setBackground(seleced);
            rlTwo.setBackground(normal);
            rlFour.setBackground(normal);
            tvTitleThree.setTextColor(color_white);
            tvContentThree.setTextColor(color_white);
            tvTitleTwo.setTextColor(color_normal_title);
            tvContentTwo.setTextColor(color_content_normal);
            tvTitleFour.setTextColor(color_normal_title);
            tvContentFour.setTextColor(color_content_normal);
        }
        presenter.getRoleList();

    }

    @Override
    public CHooseRoleView getMvpView() {
        return this;
    }

    @Override
    public ChooseRolePresenter createPresenter() {
        return new ChooseRolePresenter();
    }

    @Override
    public void onError(String message) {
        if (!TextUtils.isEmpty(message) && message.contains("完善")) {
            startActivity(new Intent(this, BasicUserInfoActivity.class).putExtra("from", 2));
            finish();
        } else {
            ToastUtils.showShort(message);
        }
    }

    @OnClick({R.id.rl_role_one, R.id.rl_role_two, R.id.rl_role_three, R.id.rl_role_four, R.id.tv_enter})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_role_one:
                ToastUtils.showShort("农户功能即将上线！");
                break;
            case R.id.rl_role_two:
                if (nowRole == ROLE_OWNER) {
                    ToastUtils.showShort("请选择其他角色");
                    return;
                }
                if (lastRole == ROLE_OWNER) {
                    return;
                }
                lastRole = ROLE_OWNER;
                rlTwo.setBackground(seleced);
                rlThree.setBackground(normal);
                rlFour.setBackground(normal);
                tvTitleTwo.setTextColor(color_white);
                tvContentTwo.setTextColor(color_white);
                tvTitleThree.setTextColor(color_normal_title);
                tvContentThree.setTextColor(color_content_normal);
                tvTitleFour.setTextColor(color_normal_title);
                tvContentFour.setTextColor(color_content_normal);
                roleId = (int) rlTwo.getTag();
                break;
            case R.id.rl_role_three:
                if (nowRole == ROLE_DIRVER) {
                    ToastUtils.showShort("请选择其他角色");
                    return;
                }
                if (lastRole == ROLE_DIRVER) {
                    return;
                }
                lastRole = ROLE_DIRVER;
                rlThree.setBackground(seleced);
                rlTwo.setBackground(normal);
                rlFour.setBackground(normal);
                tvTitleThree.setTextColor(color_white);
                tvContentThree.setTextColor(color_white);
                tvTitleTwo.setTextColor(color_normal_title);
                tvContentTwo.setTextColor(color_content_normal);
                tvTitleFour.setTextColor(color_normal_title);
                tvContentFour.setTextColor(color_content_normal);
                roleId = (int) rlThree.getTag();
                break;
            case R.id.rl_role_four:
                if (nowRole == ROLE_OIL) {
                    ToastUtils.showShort("请选择其他角色");
                    return;
                }
                if (lastRole == ROLE_OIL) {
                    return;
                }
                lastRole = ROLE_OIL;
                rlFour.setBackground(seleced);
                rlTwo.setBackground(normal);
                rlThree.setBackground(normal);
                tvTitleFour.setTextColor(color_white);
                tvContentFour.setTextColor(color_white);
                tvTitleTwo.setTextColor(color_normal_title);
                tvContentTwo.setTextColor(color_content_normal);
                tvTitleThree.setTextColor(color_normal_title);
                tvContentThree.setTextColor(color_content_normal);
                roleId = (int) rlFour.getTag();
                break;
            case R.id.tv_enter:
                presenter.changeRloe(roleId);
                break;
        }
    }

    @Override
    public void showRoleList(List<RoleBean> data) {
        if (data != null && data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
//                if (i==0){
//                    tvTitleOne.setText(data.get(i).getRoleName());
//                    tvCOntentOne.setText(data.get(i).getRoleDetail());
//                    rlOne.setTag(data.get(i).getRoleId());
//                }else
                if (i == 1) {
                    tvTitleTwo.setText(data.get(i).getRoleName());
                    tvContentTwo.setText(data.get(i).getRoleDetail());
                    rlTwo.setTag(data.get(i).getRoleId());

                } else if (i == 2) {
                    tvTitleThree.setText(data.get(i).getRoleName());
                    tvContentThree.setText(data.get(i).getRoleDetail());
                    rlThree.setTag(data.get(i).getRoleId());
                } else if (i == 3) {
                    tvTitleFour.setText(data.get(i).getRoleName());
                    tvContentFour.setText(data.get(i).getRoleDetail());
                    rlFour.setTag(data.get(i).getRoleId());
                }
            }
        }
    }

    @Override
    public void chooseSuccess() {
        ActivityUtils.finishAllActivities();
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }


}
